package com.hanliukui.ai.langchain4j.assistant.xiaozhi;

import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class XiaozhiConfig {

    @Bean
    public XiaoziChatAssistant xiaoziChatAssistant(StreamingChatModel chatModel, ChatMemoryStore chatMemoryStore, XiaoziBusinessTools businessTools, ContentRetriever contentRetriever) {
        return AiServices.builder(XiaoziChatAssistant.class).streamingChatModel(chatModel)
                .chatMemoryProvider((memoryId -> MessageWindowChatMemory.builder().id(memoryId).chatMemoryStore(chatMemoryStore).maxMessages(100).build()))
                .tools(businessTools)
                .contentRetriever(contentRetriever)
                .build();
    }

    @Bean
    public ContentRetriever contentRetriever(EmbeddingStore<TextSegment> embeddingStore, EmbeddingModel embeddingModel) {
        return EmbeddingStoreContentRetriever.builder()
                .embeddingModel(embeddingModel)
                .embeddingStore(embeddingStore)
                .maxResults(5)
                .minScore(0.8)
                .build();
    }


}
